﻿using System;
using System . Collections . Generic;
using System . IO;
using System . Linq;
using System . Runtime . InteropServices . WindowsRuntime;
using Windows . Foundation;
using Windows . Foundation . Collections;
using Windows . UI . Xaml;
using Windows . UI . Xaml . Controls;
using Windows . UI . Xaml . Controls . Primitives;
using Windows . UI . Xaml . Data;
using Windows . UI . Xaml . Input;
using Windows . UI . Xaml . Media;
using Windows . UI . Xaml . Navigation;
using WenceyWang . RichMan4L . App . Logic;
using WenceyWang . RichMan4L . App . Pages . Controls;
using WenceyWang . RichMan4L . Players . Models;
using System . Threading . Tasks;

namespace WenceyWang . RichMan4L . App . Pages
{
	/// <summary>
	/// 玩家配置页
	/// </summary>
	public sealed partial class PlayerConfigPage : Page
	{

		public StartGameParameters Parameters { get; set; }

		public PlayerConfigPage ( )
		{
			InitializeComponent ( );
		}

		protected override void OnNavigatedTo ( NavigationEventArgs e )
		{
			Parameters = e . Parameter as StartGameParameters;
		}

		private void Page_Loaded ( object sender , RoutedEventArgs e )
		{
			if ( Parameters != null )
			{
				GenerateList ( );
			}
			StartStoryBoard . Begin ( );
			StartStoryBoard . Completed += StartStoryBoard_Completed;
		}

		private void StartStoryBoard_Completed ( object sender , object e )
		{
			if ( new AppSettings ( ) . OCDMode )
			{
				MainGrid . TurnOnOCDMode ( );
			}
			StartStoryBoard . Completed -= StartStoryBoard_Completed;
		}

		private void CreateGamePageButton_Click ( object sender , RoutedEventArgs e )
		{

			PageNavigateHelper . Navigate ( typeof ( CreateGamePage ) , Parameters , "Lime" , LeaveStoryBoard , BackGroundRect , Frame , RemoveControl , AddControl );

		}

		private void StartGameButton_Click ( object sender , RoutedEventArgs e )
		{


			Tuple<Task , Action> parameters = new Tuple<Task , Action> ( Task . Run ( ( ) => { new Game ( ); Game . Current . Start ( Parameters ); } ) , ( ) => { } );

			PageNavigateHelper . Navigate ( typeof ( LoadingPage ) , Parameters , "Lime" , LeaveStoryBoard , BackGroundRect , Frame , RemoveControl , AddControl );
		}

		private void RemoveControl ( )
		{
			CreateGamePageButton . Click -= CreateGamePageButton_Click;
			StartGameButton . Click -= StartGameButton_Click;
		}

		private void AddControl ( )
		{
			CreateGamePageButton . Click += CreateGamePageButton_Click;
			StartGameButton . Click += StartGameButton_Click;
		}

		private void AddButton_Click ( object sender , RoutedEventArgs e )
		{
			var modelList = PlayerModelProxy . GetPlayerModels ( );


			PlayerConfigListItem toAdd = new PlayerConfigListItem ( );
			toAdd . Margin = new Thickness ( 0 , 20 , 0 , 0 );
			toAdd . Delete += PlayerConfigListItem_Delete;
			toAdd . PlayerModelName = ( modelList . FirstOrDefault ( ) ) . Name;

			PlayerConfigStackPanel . Children . Add ( toAdd );
			NamePlayer ( );
		}

		private void PlayerConfigListItem_Delete ( object sender , RoutedEventArgs e )
		{
			PlayerConfigStackPanel . Children . Remove ( sender as UIElement );
			NamePlayer ( );
		}

		private void NamePlayer ( )
		{
			int number = 1;
			foreach ( var item in PlayerConfigStackPanel . Children )
			{
				if ( item is PlayerConfigListItem )
				{
					( item as PlayerConfigListItem ) . PlayerName = "玩家 " + number . ToString ( );
					number++;
				}
			}
		}

		private void GenerateParameters ( )
		{
			Parameters . PlayerConfig = new List<Tuple<PlayerModelProxy , Players . PlayerType>> ( );
			foreach ( var item in PlayerConfigStackPanel . Children )
			{
				if ( item is PlayerConfigListItem )
				{

					Parameters . PlayerConfig . Add ( new Tuple<PlayerModelProxy , Players . PlayerType> ( ( item as PlayerConfigListItem ) . PlayerModel , ( Players . PlayerType ) Enum . Parse ( typeof ( Players . PlayerType ) , ( item as PlayerConfigListItem ) . Controller ) ) );

				}

			}


		}
		private void GenerateList ( )
		{
			foreach ( var item in Parameters . PlayerConfig )
			{
				PlayerConfigListItem toAdd = new PlayerConfigListItem ( );
				toAdd . PlayerModelName = item . Item1 . Name;
				toAdd . Controller = item . Item2 . ToString ( );
				PlayerConfigStackPanel . Children . Add ( toAdd );

			}
			NamePlayer ( );
		}
	}
}
